package cn.wolfcode.p2p.website.controller;

import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.qo.PaymentScheduleQueryObject;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.base.util.UserContext;
import cn.wolfcode.p2p.business.service.IPaymentScheduleService;
import cn.wolfcode.p2p.util.RequireLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ReturnMoneyController {
    @Autowired
    private IPaymentScheduleService paymentScheduleService;
    @Autowired
    private IAccountService accountService;

    @RequestMapping("borrowBidReturn_list")
    @RequireLogin
    public String returnMoneyPage(@ModelAttribute("qo") PaymentScheduleQueryObject qo, Model model){
        qo.setBorrowUserId(UserContext.getCurrent().getId());
        model.addAttribute("pageInfo",paymentScheduleService.queryPage(qo));
        model.addAttribute("account", accountService.getCurrent());
        return "/returnmoney_list";
    }

    @RequestMapping("/returnMoney")
    @ResponseBody
    public JSONResult returnMoney(Long id){
        JSONResult result = null;
        try {
            paymentScheduleService.returnMoney(id);
            result = new JSONResult("");
        } catch (DisplayableException ex) {
            //异常需要在前台显示
            ex.printStackTrace();
            result = new JSONResult(false, ex.getMessage());
        } catch (Exception e){
            //异常记录到数据库或日志文件中
            e.printStackTrace();
            result = new JSONResult(false, "系统异常,请联系管理员");
        }
        return result;
    }
}
